package com.dxx.cloud.admin.controller.tp;

import com.github.binarywang.wxpay.bean.notify.SignatureHeader;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyV3Response;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyV3Result;
import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyV3Result;
import com.github.binarywang.wxpay.bean.request.WxPayRefundQueryV3Request;
import com.github.binarywang.wxpay.bean.result.WxPayRefundQueryV3Result;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/wxpay")
public class WxPayController {
    @Resource
    private WxPayService wxService;


//    @Operation(summary = "退款")
//    @PostMapping("/refund")
//    public WxPayRefundV3Result refund(@RequestBody WxPayRefundV3Request request) throws WxPayException {
//        return this.wxService.refundV3(request);
//    }

    /**
     * <pre>
     * 微信支付-查询退款
     * 详见 https://pay.weixin.qq.com/doc/v3/merchant/4012791863
     * </pre>
     *
     * @param outRefundNo 商户退款单号
     * @return 退款信息
     */
    @Operation(summary = "退款查询")
    @GetMapping("/refundQuery")
    public WxPayRefundQueryV3Result refundQuery(@RequestParam("outRefundNo") String outRefundNo)
            throws WxPayException {
        return this.wxService.refundQueryV3(outRefundNo);
    }

    @Operation(summary = "退款查询")
    @PostMapping("/refundQuery")
    public WxPayRefundQueryV3Result refundQuery(@RequestBody WxPayRefundQueryV3Request wxPayRefundQueryV3Request) throws WxPayException {
        return this.wxService.refundQueryV3(wxPayRefundQueryV3Request);
    }

    /**
     * <pre>
     * 支付成功回调
     * 详见 https://pay.weixin.qq.com/doc/v3/merchant/4012791861
     * </pre>
     *
     * @param notifyData
     * @param request
     * @return
     * @throws WxPayException
     */
    @Operation(summary = "支付回调通知处理")
    @PostMapping("/notify/order")
    public ResponseEntity<String> parseOrderNotifyResult(@RequestBody String notifyData, HttpServletRequest request) {
        SignatureHeader header = getRequestHeader(request);
        try {
            WxPayNotifyV3Result res = this.wxService.parseOrderNotifyV3Result(notifyData, header);
            WxPayNotifyV3Result.DecryptNotifyResult decryptRes = res.getResult();
            // TODO 根据自己业务场景需要构造返回对象
            if (WxPayConstants.WxpayTradeStatus.SUCCESS.equals(decryptRes.getTradeState())) {
                //成功返回200/204，body无需有内容
                return ResponseEntity.status(200).body("");
            } else {
                //失败返回4xx或5xx，且需要构造body信息
                return ResponseEntity.status(500).body(WxPayNotifyV3Response.fail("错误原因"));
            }
        } catch (WxPayException e) {
            //失败返回4xx或5xx，且需要构造body信息
            return ResponseEntity.status(500).body(WxPayNotifyV3Response.fail("错误原因"));
        }
    }

    /**
     * <pre>
     * 支付成功回调
     * 详见 https://pay.weixin.qq.com/doc/v3/merchant/4012791865
     * </pre>
     *
     * @param notifyData
     * @param request
     * @return
     * @throws WxPayException
     */
    @Operation(summary = "退款回调通知处理")
    @PostMapping("/notify/refund")
    public ResponseEntity<String> parseRefundNotifyResult(@RequestBody String notifyData, HttpServletRequest request) {
        SignatureHeader header = getRequestHeader(request);
        try {
            WxPayRefundNotifyV3Result res = this.wxService.parseRefundNotifyV3Result(notifyData, header);
            WxPayRefundNotifyV3Result.DecryptNotifyResult decryptRes = res.getResult();
            // TODO 根据自己业务场景需要构造返回对象
            if (WxPayConstants.RefundStatus.SUCCESS.equals(decryptRes.getRefundStatus())) {
                //成功返回200/204，body无需有内容
                return ResponseEntity.status(200).body("");
            } else {
                //失败返回4xx或5xx，且需要构造body信息
                return ResponseEntity.status(500).body(WxPayNotifyV3Response.fail("错误原因"));
            }
        } catch (WxPayException e) {
            //失败返回4xx或5xx，且需要构造body信息
            return ResponseEntity.status(500).body(WxPayNotifyV3Response.fail("错误原因"));
        }
    }

    /**
     * 组装请求头重的前面信息
     *
     * @param request
     * @return
     */
    private SignatureHeader getRequestHeader(HttpServletRequest request) {
        // 获取通知签名
        String signature = request.getHeader("Wechatpay-Signature");
        String nonce = request.getHeader("Wechatpay-Nonce");
        String serial = request.getHeader("Wechatpay-Serial");
        String timestamp = request.getHeader("Wechatpay-Timestamp");

        SignatureHeader signatureHeader = new SignatureHeader();
        signatureHeader.setSignature(signature);
        signatureHeader.setNonce(nonce);
        signatureHeader.setSerial(serial);
        signatureHeader.setTimeStamp(timestamp);
        return signatureHeader;
    }
}
